﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpHomeEngine;

namespace Insteon.Devices
{

    public class MotionSensor : InsteonDevice
    {
        TagBoolean MotionDetected;

        public MotionSensor(InsteonInterface Interface, string Name, InsteonAddress Address)
            : base(Interface, Name, Address)
        {
            MotionDetected = new TagBoolean(Interface, this, "Motion");
        }

        internal override Commands.Command GenerateStateChangeCommand(Tag Tag, object Value)
        {
            //I dont do tag state change requests
            return null;
        }


        internal override void HandleUnsolicitedMessage(Messages.Message Message)
        {
//#02 #50 #14 #c0 #cc #00 #00 #01 #cb #11 #01 
//163349.780  0039 StandardMessage      (to:00.00.01) (fr:14.C0.CC) (Flags: AllLinkBroadcast False 2 3) (c1:11 c2: 1)     : 02 50 14 c0 cc 00 00 01 cb 11 01  Unsolicited
//#02 #50 #14 #c0 #cc #00 #00 #01 #cb #11 #01 
//163349.972  0040 StandardMessage      (to:00.00.01) (fr:14.C0.CC) (Flags: AllLinkBroadcast False 2 3) (c1:11 c2: 1)     : 02 50 14 c0 cc 00 00 01 cb 11 01  Unsolicited
//#02 #50 #14 #c0 #cc #aa #aa #aa #41 #11 #01 
//163402.345  0041 StandardMessage      (to:AA.AA.AA) (fr:14.C0.CC) (Flags: ALLLinkCleanup   False 0 1) (c1:11 c2: 1)     : 02 50 14 c0 cc aa aa aa 41 11 01  Unsolicited
//#02 #50 #14 #c0 #cc #11 #02 #01 #cb #06 #01 
//163402.554  0042 StandardMessage      (to:11.02.01) (fr:14.C0.CC) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 1)     : 02 50 14 c0 cc 11 02 01 cb 06 01  Unsolicited
//#02 #50 #14 #c0 #cc #11 #02 #01 #cb #06 #01 
//163402.745  0043 StandardMessage      (to:11.02.01) (fr:14.C0.CC) (Flags: AllLinkBroadcast False 2 3) (c1: 6 c2: 1)     : 02 50 14 c0 cc 11 02 01 cb 06 01  Unsolicited

            if (Message is Messages.Insteon.StandardMessage)
            {
                Messages.Insteon.StandardMessage Mess = (Messages.Insteon.StandardMessage)Message;
                if (Mess.From == this.InsteonAddress)
                {
                    if (Mess.Command1 == 0x13)
                    {
                        //Door Closing
                        //this.InsteonInterface.
                        UnsolicitedTagChange(MotionDetected, false);
                    }
                    if (Mess.Command1 == 0x11)
                    {
                        //Door opening
                        UnsolicitedTagChange(MotionDetected, true);
                    }
                }
            }

        }

        internal override Commands.Command GenerateStateRequest(Tag Tag)
        {
            //triggerlinc is rf only and they go into a power saving mode where they cant be queried.
            //So this wont do anything.
            //and get's will only timeout becuase the device wont reply.
            if (Tag == MotionDetected)
            {
                Commands.Insteon.StandardCommand Command = new Commands.Insteon.StandardCommand(this.InsteonAddress, 0x19, 0x00);
                return Command;
            }
            return null;
        }


        internal override object ReadStateRequestResponse(Commands.Command Command, Tag Tag)
        {
            if (Command.DeviceResponse == null)
            {
                return null;
            }
            if (!(Command.DeviceResponse is Messages.Insteon.StandardMessage))
            {
                return null;
            }
            Messages.Insteon.StandardMessage Message = (Messages.Insteon.StandardMessage)Command.DeviceResponse;
            if (Tag == MotionDetected)
            {
                Command.Value = MotionDetected.CastValue(Message.Command2);
            }
            return null;
        }
    }
}
